package com.sann.eduservice.controller.front;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sann.common.utils.R;
import com.sann.eduservice.entity.course.Course;
import com.sann.eduservice.entity.teacher.Teacher;
import com.sann.eduservice.service.CourseService;
import com.sann.eduservice.service.TeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(tags = "网站展示接口服务,跟edu公用同一个数据库，因此接口放在同一个微服务下面")
@RestController
@RequestMapping(path = "/eduservice/front/web")
@CrossOrigin
public class WebController {
    @Autowired
    private CourseService courseService;

    @Autowired
    private TeacherService teacherService;

    @ApiOperation(value = "获取首页热门课程和讲师")
    @GetMapping(path = "/getHotCourseAndTeacherList")
    public R getHotCourseAndTeacherList(){
        List<Course> courseList = null;
        List<Teacher> teacherList = null;
        try {
            // 查询前8条热门课程，这里就是简单根据id逆序查询数据库前8条数据
            QueryWrapper<Course> courseQueryWrapper = new QueryWrapper<>();
            courseQueryWrapper.orderByDesc("id");
            courseQueryWrapper.last("limit 8");
            courseList = this.courseService.list(courseQueryWrapper);

            // 这里也是简单查询数据库前4条数据
            QueryWrapper<Teacher> teacherQueryWrapper = new QueryWrapper<>();
            teacherQueryWrapper.orderByDesc("id");
            teacherQueryWrapper.last("limit 4");
            teacherList = this.teacherService.list(teacherQueryWrapper);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return R.ok().data("courseList", courseList).data("teacherList", teacherList);
    }

}
